/*
题目链接：https://leetcode.cn/problems/minimum-number-of-days-to-make-m-bouquets/description/
1482. 制作m束花所需的最少天数-中等
完成日期：2024/9/24
二分搜索
*/
class Solution {
public:
    int minDays(vector<int>& bloomDay, int m, int k) {
        int n=bloomDay.size();
        if(m>n/k){
            return -1;
        }   
        int l=*min_element(bloomDay.begin(),bloomDay.end());
        int r=*max_element(bloomDay.begin(),bloomDay.end());
        while(l<r){
            int q=0;
            int p=0;
            int mid = (l + r)/2;
            for(int c:bloomDay){
                if(c<=mid){
                    q++;
                    if(q == k){
                        p++;
                        q=0;
                    }
                }else{
                    q=0;
                }
            }
            if(p < m){
                l = mid+1;
            }else{
                r = mid;
            }
        }
        return l;

    }
};